<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/9/7
 * Time: 22:09
 */

namespace app\controllers;

use app\models\Comment;
use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\User1;
use \yii\web\Response;

class CommentController extends Controller {
    public $enableCsrfValidation = false;
    public function behaviors()
    {
        return [

            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    public function actionLogin()
    {

        $model = new User1();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $connection=Yii::$app->db2;
            $command = $connection->createCommand('SELECT * FROM user1 where username=:1 and password=:2');
            $command=$command->bindValue(':1',$model['username']);
            $command=$command->bindValue(':2',$model['password']);
            $result = $command->queryOne();

            $connection1=Yii::$app->db2;
            $command1 = $connection1->createCommand('SELECT * FROM comment');
            $result1 = $command1->queryAll();

            if($result!=null) {
                $session=Yii::$app->session;
                $session['user']=$result;
                $session['comment']=$result1;
                return   $this->render('comment', [
                    'comment' => $result1,]);
            }
            else  return  $this->render('login', [
                'model' => $model,
            ]);
        }
        else  return  $this->render('login', [
            'model' => $model,
        ]);
    }




public function actionRegister()
{
    $model = new User1();
    if ($model->load(Yii::$app->request->post()) && $model->validate()) {
        $connection=Yii::$app->db2;
        $command = $connection->createCommand('insert into user1(username,password) Values (:1,:2)');
        $command=$command->bindValue(':1',$model['username']);
        $command=$command->bindValue(':2',$model['password']);
        $result = $command->execute();
        if($result!=0) {
            return $this->redirect('http://localhost/yii/web/index.php?r=comment/login');
        }
        else  return  $this->render('register', [
            'model' => $model,]);
    }
    else  return  $this->render('register', [
        'model' => $model,
    ]);
}
    public function actionComment()
    {
    $session=Yii::$app->session;
    return  $this->render('comment', [
        'comment' => $session['comment']]);
    }
    public function actionSend()
    {
        $comment=new Comment();
        $session=Yii::$app->session;
        if($comment->context=='')
        {
            $comment->pid=Yii::$app->request->post('pid');
            $comment->username=$session['user']['username'];
            $comment->time=date("Y M D H:i:s", time() + 21600);
            $comment->context=Yii::$app->request->post('context');

            $connection=Yii::$app->db2;
            $command = $connection->createCommand('insert into  comment(pid,username,time,context) Values (:1,:2,:3,:4)');
            $command=$command->bindValue(':1',$comment->pid);
            $command=$command->bindValue(':2',$comment->username);
            $command=$command->bindValue(':3',$comment->time);
            $command=$command->bindValue(':4',$comment->context);
            $command->execute();

            $command1 = $connection->createCommand("SELECT MAX(id) FROM comment");
            $id= $command1->queryScalar();
            $comment->id=$id;
            return json_encode($comment);
        }
        else return   $this->render('comment');
    }
    public function actionAnswer()
    {
        $comment=new Comment();
        $session=Yii::$app->session;
        if($comment->context=='')
        {
            $comment->pid=Yii::$app->request->post('pid');
            $comment->username=Yii::$app->request->post('username');
            $comment->time=date("Y M D H:i:s", time() + 21600);
            $comment->context=Yii::$app->request->post('context');

            $connection=Yii::$app->db2;
            $command = $connection->createCommand('insert into  comment(pid,username,time,context) Values (:1,:2,:3,:4)');
            $command=$command->bindValue(':1',$comment->pid);
            $command=$command->bindValue(':2',$comment->username);
            $command=$command->bindValue(':3',$comment->time);
            $command=$command->bindValue(':4',$comment->context);
            $command->execute();
            $command1 = $connection->createCommand("SELECT MAX(id) FROM comment");
            $id= $command1->queryScalar();
            $comment->id=$id;
            return json_encode($comment);

        }
else return   $this->renderAjax('comment');
    }
}